package leetcode.easy;

public class $414_ThirdMax {
    public int thirdMax(int[] nums) {
        long max = Long.MIN_VALUE;
        long sec = Long.MIN_VALUE;
        long thir = Long.MIN_VALUE;
        for (long x : nums) {
            if (x > max) {
                thir = sec;
                sec = max;
                max = x;
            } else if (x > sec && x < max) {
                thir = sec;
                sec = x;
            } else if (x > thir && x < sec) {
                thir = x;
            }
        }
        return thir == Long.MIN_VALUE ? (int) max : (int) thir;
    }
}
